package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.inst.ProdInstService;
import com.iwhalecloud.bss.kite.cucc.web.service.accept.util.ConnectProdUtil;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.bo.LoginInfo;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName : TransPlanHandler
 * @Description : 转移至企业虚拟ID
 * @Author : chen.cheng110
 * @Date : 2020/12/19 11:37
 */
@Service
public class TransPlanHandler extends AbstractAttrHandler {
    @Resource
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {

    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(sceneInst.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(sceneInstId, LocalKeyConsts.BUSI_CHANGE_SCENE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(sceneInstId, "initSource"))) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        if (KiteStringUtils.isEmpty(value)) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        LoginInfo loginInfo = ContextUtil.getLoginInfo();
        Map<String, Object> extParams = loginInfo.getUserInfo().getExtParams();
        //重提
        if (Objects.nonNull(extParams) && extParams.containsKey("nodeCode")) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        //工号
        String staffCode = (String) ContextUtil.getLoginInfo().getUserInfo().getExtParams().get("account");
        //省份编码
        String provinceCode = loginInfo.getUserInfo().getPostProvinceNbr();
        String eparchyCode = loginInfo.getUserInfo().getPostLanId();
        String useAccNum = "";
        // 判断转移至企业虚拟ID是否是在用的业务号码
        useAccNum = SpringUtil.getBean(ProdInstService.class)
            .queryUseAccNum(value, staffCode, provinceCode, eparchyCode);
        IOperateSceneCommonService operateSceneCommonService = SpringUtil.getBean(IOperateSceneCommonService.class);

        Map<String, Object> map = ConnectProdUtil.getPlanNbrsByAccNum(sceneInst.getCustId(), value);
        List<String> communicationPlanNbrs = (List<String>) map.get("tx_plan");
        List<String> tariffPlanNbrs = (List<String>) map.get("zf_plan");

        KiteAttr transPlanTx = null;
        KiteAttr transPlanZf = null;
        // ORD10183 转移至企业虚拟ID ORD10191 迁移至的账户虚拟ID
        if (KiteStringUtils.equals("ORD10183", fieldName)) {
            // ORD10201 转移至的通信计划 ORD10202 转移至的资费计划
            transPlanTx = querySceneInstService.getCommonAttr(sceneInstId, "ORD10201");
            transPlanZf = querySceneInstService.getCommonAttr(sceneInstId, "ORD10202");
        } else if (KiteStringUtils.equals("ORD10191", fieldName)) {
            // ORD10231 迁移至的资费计划
            transPlanZf = querySceneInstService.getCommonAttr(sceneInstId, "ORD10231");
        }
        if (Objects.nonNull(transPlanTx)) {
            List<KiteAttrValue> attrValuesTx = new ArrayList<>();
            communicationPlanNbrs.forEach(number -> {
                KiteAttrValue kiteAttrValue = new KiteAttrValue();
                kiteAttrValue.setAttrValue(number);
                kiteAttrValue.setAttrValueName(number);
                attrValuesTx.add(kiteAttrValue);
            });
            transPlanTx.setAttrValues(attrValuesTx);
            //联动以后把原来的值置空
            transPlanTx.setValue(null);
            transPlanTx.setValueDesc(null);
            operateSceneCommonService.setCommonAttr(sceneInstId, transPlanTx);
        }

        if (Objects.nonNull(transPlanZf)) {
            List<KiteAttrValue> attrValuesZf = new ArrayList<>();
            tariffPlanNbrs.forEach(number -> {
                KiteAttrValue kiteAttrValue = new KiteAttrValue();
                kiteAttrValue.setAttrValue(number);
                kiteAttrValue.setAttrValueName(number);
                attrValuesZf.add(kiteAttrValue);
            });
            transPlanZf.setAttrValues(attrValuesZf);
            //联动以后把原来的值置空
            transPlanZf.setValue(null);
            transPlanZf.setValueDesc(null);
            operateSceneCommonService.setCommonAttr(sceneInstId, transPlanZf);
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
